"""Definition of the Social content type
"""
from collective.recaptchawidget.field import CaptchaField

from zope.interface import implements, directlyProvides

from Products.Archetypes import atapi
from Products.ATContentTypes.content import base
from Products.ATContentTypes.content import schemata

from newguegue.newmunicipios import newmunicipiosMessageFactory as _
from newguegue.newmunicipios.interfaces import ISocial
from newguegue.newmunicipios.config import PROJECTNAME

SocialSchema = schemata.ATContentTypeSchema.copy() + atapi.Schema((

    # -*- Your Archetypes field definitions here ... -*-
   atapi.StringField('title',
                searchable=1,
                isMetadata=0,
                required=1,
                accessor='Title',
                widget=atapi.StringWidget(label='Título de social',
                                      maxlength=150,
                                      size=30,
                                      label_msgid="label_entry_titles",
                                      description_msgid="help_entry_title",
                                      i18n_domain="newguegue.newmunicipios",
                                      description='Titulo del social es requerido')),
    atapi.StringField('description',
                searchable=1,
                isMetadata=0,
                required=1,
                accessor='Description',
                widget=atapi.TextAreaWidget(label='Breve descripción de social',
                                      label_msgid="label_entry_descriptions",
                                      description_msgid="help_entry_description",
                                      i18n_domain="newguegue.newmunicipios",
                                      description='Entre una breve descripción.')),
     atapi.DateTimeField('fecha',
                 searchable=0,
                 required=0,
                 widget=atapi.CalendarWidget(label='Fecha de social',
                                       label_msgid="label_entry_fechas",
                                       show_hm = False,
                                       description_msgid="help_entry_fecha",
                                       i18n_domain="newguegue.newmunicipios",
                                       description='Fecha')),
    atapi.StringField('contacto',
                searchable=0,
                isMetadata=0,
                required=1,
                widget=atapi.StringWidget(label='Nombre del contacto',
                                      label_msgid="label_entry_contacto",
                                      description_msgid="help_entry_contacto",
                                      i18n_domain="newguegue.newmunicipios",
                                      description='Nombre del contacto')),
    atapi.StringField('telefono',
                searchable=0,
                isMetadata=0,
                required=0,
                widget=atapi.StringWidget(label='Teléfono del contacto',
                                      label_msgid="label_entry_telefono",
                                      description_msgid="help_entry_telefono",
                                      i18n_domain="newguegue.newmunicipios",
                                      description='Telefono del contacto')),
    atapi.StringField('fax',
                searchable=1,
                isMetadata=0,
                widget=atapi.StringWidget(label='Fax del contacto',
                                      label_msgid="label_entry_fax",
                                      description_msgid="help_entry_fax",
                                      i18n_domain="newguegue.newmunicipios",
                                      description='Fax del contacto')),

    atapi.StringField('emailc',
                searchable=1,
                isMetadata=0,
                required=1,
                validators=('isEmail'),
                widget=atapi.StringWidget(label='Email del contacto',
                                      label_msgid="label_entry_emailc",
                                      description_msgid="help_entry_emailc",
                                      i18n_domain="newguegue.newmunicipios",
                                      description='Email del contacto')),
    atapi.StringField('email',
                searchable=1,
                isMetadata=1,
                required=1,
                validators=('isEmail'),
                widget=atapi.StringWidget(label='Email del que sube el clasificado',
                                      label_msgid="label_entry_email",
                                      description_msgid="help_entry_email",
                                      i18n_domain="newguegue.newmunicipios",
                                      description='Email del que sube el clasificado')),
    atapi.ImageField('image',
              searchable=0,
              required=0,
              sizes= {'large'   : (768, 768),
                'preview' : (400, 400),
                'mini'    : (200, 200),
                'thumb'   : (128, 128),
                'tile'    :  (64, 64),
                'icon'    :  (32, 32),
                'listing' :  (16, 16),
               },
              widget=atapi.ImageWidget(label='Imagen',
                                label_msgid="label_entry_image",
                                description_msgid="help_entry_image",
                                i18n_domain="newguegue.newmunicipios",
                                threshold=102400,
                                description='Imagen relacionada al clasificado.')),
    atapi.TextField('body',
              searchable=1,
              required=1,
              primary=1,
              default_content_type='text/html',
              default_output_type='text/html',
              allowable_content_types=('text/plain','text/structured', 'text/html', 'text/restructured'),
              widget=atapi.RichWidget(label='Detalle',
                                label_msgid="label_entry_body",
                                description_msgid="help_entry_body",
                                i18n_domain="newguegue.newmunicipios",
                                description="Descripción completa")),
    atapi.StringField('preguntaseguridad',
                required = 1,
                validators = ('validatethis',),
                widget=atapi.StringWidget(label='¿Cual es la capital de Nicaragua?',
                                          label_msgid="label_segurity",
                                          descripction_msgid="help_entry_segurity",
                                          i18n_domain="newguegue.newmunicipios",
                                          description="Pregunta de seguridad")),
  CaptchaField(
              'captcha',
      ),
))


# Set storage on fields copied from ATContentTypeSchema, making sure
# they work well with the python bridge properties.

#SocialSchema['title'].storage = atapi.AnnotationStorage()
#SocialSchema['description'].storage = atapi.AnnotationStorage()

schemata.finalizeATCTSchema(SocialSchema, moveDiscussion=False)

class Social(base.ATCTContent):
    """Social"""
    implements(ISocial)

    portal_type = "Social"
    schema = SocialSchema

    def tag(self, **kwargs):
        """Generate image tag using the api of the ImageField
        """
        #if 'title' not in kwargs:
        #    kwargs['title'] = self.getImageCaption()
        return self.getField('image').tag(self, **kwargs)

    def getUnpublishedEntries(self, social):
        states = self.getEntryWorkflowStates(social)
        pubstate = query['publico']# self.getPublishedState()
        states = [s for s in states if s!=pubstate]
        query={'review_state':states}
        entries = self.searchForEntries(blog, filterState=0, maxResults=0, fromHere=1, **query)
        return entries

 #   title = atapi.ATFieldProperty('title')
 #   description = atapi.ATFieldProperty('description')

atapi.registerType(Social, PROJECTNAME)

